package br.edu.fatec.ratoninja.deprecated;

import br.edu.fatec.ratoninja.GameView;
import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class GameLoop extends Thread {
	private static final String TAG = "RatoNinja::GameLoop";
	private GameView gameView;
	private boolean isRun;
	private SurfaceHolder surfaceHolder;

	public GameLoop(GameView gameView, SurfaceHolder surfaceHolder) {
		this.gameView = gameView;
		this.surfaceHolder = surfaceHolder;
		this.isRun = false;
	}
	
	public void setRunning(boolean value) {
		this.isRun = value;
	}

	@Override
	public void run() {
		Canvas canvas;
		while (isRun) {
			canvas = null;
			try {
				canvas = surfaceHolder.lockCanvas(null);
				synchronized (surfaceHolder) {
					gameView.updatePhysics();
					//Log.i(TAG, "isRun - onDraw");
					gameView.invalidate();
				}
			} finally {
				if (canvas != null) {
					surfaceHolder.unlockCanvasAndPost(canvas);
				}
			}
		}
	}

	public SurfaceHolder getSurfaceHolder() {
		return surfaceHolder;
	}
}
